package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Long> iLike(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
    }

    @Override
    public List<Long> likeMe(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return CollUtil.getFieldValues(userLikes, "userId", Long.class);
    }

    @Override
    public Map<String, Integer> counts(Long userId) {
        Map<String, Integer> map = new HashMap<>();
        //查询我喜欢的
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> iLike = mongoTemplate.find(query1, UserLike.class);
        if (!CollUtil.isEmpty(iLike)) {
            map.put("loveCount", iLike.size());
        } else {
            map.put("loveCount", 0);
        }
        //查询喜欢我的
        Query query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> likeMe = mongoTemplate.find(query2, UserLike.class);
        if (!CollUtil.isEmpty(likeMe)) {
            map.put("fanCount", likeMe.size());
        } else {
            map.put("fanCount", 0);
        }
        List<Long> likeUserIds = CollUtil.getFieldValues(iLike, "likeUserId", Long.class);
        //查询互相喜欢的
        Query query3 = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId)
                .and("isLike").is(true));
        List<UserLike> likeEachOther = mongoTemplate.find(query3, UserLike.class);
        if (!CollUtil.isEmpty(likeEachOther)) {
            map.put("eachLoveCount", likeEachOther.size());
        } else {
            map.put("eachLoveCount", 0);
        }
        return map;
    }

}